alter procedure p_updateusedqty
(
  @depotid int,
  @productid int,
  @quantity decimal(18,4),
  @custom1 varchar(200),
  @custom2 varchar(200),
  @custom3 varchar(200),
  @length decimal(18,8),
  @width decimal(18,8),
  @height decimal(18,8)
)
as
  declare @industrydepotinfo bit
begin
  if @depotid is null 
    return
  select @industrydepotinfo=p_paramvalue from ts_parameters where p_paramnm='industrydepotinfo'
  if not exists(select 1 from tt_book where depotid=@depotid and productid=@productid 
    and ((@industrydepotinfo=0) or (@industrydepotinfo=1 
    and isnull(custom1,'')=isnull(@custom1,'') and isnull(custom2,'')=isnull(@custom2,'') and isnull(custom3,'')=isnull(@custom3,'')
    and isnull(length,0)=isnull(@length,0) and isnull(width,0)=isnull(@width,0) and isnull(height,0)=isnull(@height,0))))
    insert into tt_book (depotid, productid, custom1, custom2, custom3, length, width, height) 
      values (@depotid, @productid, isnull(@custom1,''), isnull(@custom2,''), isnull(@custom3,''), 
                                    isnull(@length,0), isnull(@width,0), isnull(@height,0))

  update tt_book set usedqty=isnull(usedqty,0)+@quantity
    where depotid=@depotid and productid=@productid 
      and ((@industrydepotinfo=0) or (@industrydepotinfo=1 
       and isnull(custom1,'')=isnull(@custom1,'') and isnull(custom2,'')=isnull(@custom2,'') and isnull(custom3,'')=isnull(@custom3,'')
       and isnull(length,0)=isnull(@length,0) and isnull(width,0)=isnull(@width,0) and isnull(height,0)=isnull(@height,0)))    
end